package com.wechat.back.config;

import com.wechat.back.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Webconfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        //登录接口和注册接口放行
        registry.addInterceptor(loginInterceptor).excludePathPatterns(
                "/admin/login",
                "/user/login",
                "/admin/register",
                "/user/register"
        );
    }

    /**
     * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片等
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
            配置server虚拟路径，handler为前台访问的URL目录，locations为files相对应的本地路径
            也就是说如果有一个 upload/avatar/aaa.png 请求，那程序会到后面的目录里面找aaa.png文件
            另外：如果项目中有使用Shiro，则还需要在Shiro里面配置过滤下
         */
        registry.addResourceHandler("/upload/avatar/**")
                .addResourceLocations("file:F:\\upload\\avatar\\");

        registry.addResourceHandler("/upload/icons/**")
                .addResourceLocations("file:F:\\upload\\icons\\");

        registry.addResourceHandler("/upload/bg/**")
                .addResourceLocations("file:F:\\upload\\bg\\");
    }
}
